package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {

	srv := gin.Default()
	srv.POST("/sms/send", func(c *gin.Context) {
		phone := c.DefaultPostForm("Phone", "")

		fields := Fields{
			{name: "手机号", funcNames: []string{IsRequire, IsNumber, IsPhone}, value: phone},
		}

		if err := fields.validate(); err != nil {
			c.JSON(http.StatusOK, err)
			return
		}

		c.JSON(http.StatusOK, gin.H{"Phone": phone})
	})

	srv.POST("/user/sms/login", func(c *gin.Context) {
		phone := c.DefaultPostForm("Phone", "")
		code := c.DefaultPostForm("Code", "")

		fields := Fields{
			{name: "手机号", funcNames: []string{IsRequire, IsNumber, IsPhone}, value: phone},
			{name: "验证码", funcNames: []string{IsRequire, IsNumber, Length}, value: code, args: []any{4}},
		}

		if err := fields.validate(); err != nil {
			c.JSON(http.StatusOK, err)
			return
		}

		c.JSON(http.StatusOK, gin.H{"Phone": phone, "Code": code})
	})

	srv.Run(":80")
}
